#!/usr/bin/env python3

import pymongo

client = pymongo.MongoClient('localhost', 27017)

# 创建数据库，名为gyl. 类比excel文件的文件名
gyl = client['gyl']

# 创建表，名为sheet_line. 类比excel文件的工作表
sheet_line = gyl['sheet_line']

def insert_data():
    with open('a.txt', 'r') as f:
        lines = f.readlines()
        for index, line in enumerate(lines):
            data = {
                    'index' : index,
                    'line' : line,
                    'words' : len(line.split())
                    }
            sheet_line.insert_one(data)

insert_data()

print("Get all")
for item in sheet_line.find():
    print(item['line'])

print("Get all that words == 3")
for item in sheet_line.find({'words' : 3}):
    print(item)

print("Get all that words <= 5")
# $lt/$lte/$gt/$gte/$ne 依次等价于 </<=/>/>=/!=, (l表示less，g表示greater，e表示queal，n表示not)
for item in sheet_line.find({'words' : {'$lt':5}}):
    print(item)
